﻿// 1.1.7.10. Write a program, which finds the most frequently occurring element in an array.
// Example: {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} -> 4 (5 times).

using System;
//using System.Collections.Generic;
using System.Linq;

class MostFrequentlyOccurringElement
{
    static void Main()
    {   
        //List<object> list4e = new List<object>() { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 };
        object[] listy = new object[] { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 };
        //List<int> counters = new List<int>(list4e.Distinct().Count());
        int[] counters = new int[listy.Distinct().Count()];
        object[] uniqueElements = new object[listy.Distinct().Count()];
        uniqueElements = listy.Distinct().ToArray();
        for (int i = 0; i < uniqueElements.GetLength(0); i++)
        {
            for (int j = 0; j < listy.GetLength(0); j++)
            {
                //if (uniqueElements[i] == listy[j]) // == does NOT work for objects
                //{
                //    counters[i]++;
                //}
                if (uniqueElements[i].Equals(listy[j]))
                {
                    counters[i]++;
                }
            }
        }
        int mostFrequent = 0;
        for (int i = 0; i < counters.GetLength(0); i++)
        {
            if (counters[i] > mostFrequent)
            {
                mostFrequent = counters[i];
            }
        }
        Console.WriteLine("{0} ({1} times)", uniqueElements[Array.IndexOf(counters, mostFrequent)], mostFrequent);
        //Console.WriteLine(uniqueElements[0]);


        //int[] arr = new int[] { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 };
        //int[] counters = new int[10];

        //for (int i = 0; i < arr.GetLength(0); i++)
        //{
        //    switch (arr[i])
        //    {
        //        case 0: counters[0]++; break;
        //        case 1: counters[1]++; break;
        //        case 2: counters[2]++; break;
        //        case 3: counters[3]++; break;
        //        case 4: counters[4]++; break;
        //        case 5: counters[5]++; break;
        //        case 6: counters[6]++; break;
        //        case 7: counters[7]++; break;
        //        case 8: counters[8]++; break;
        //        case 9: counters[9]++; break;
        //        default: Console.WriteLine("Not a digit."); break;
        //    }    
        //}

        //int mostFrequent = 0;
        //for (int i = 0; i < counters.GetLength(0); i++)
        //{
        //    if (counters[i] > mostFrequent)
        //    {
        //        mostFrequent = counters[i];
        //    }
        //}

        //Console.WriteLine("{0} ({1} times)", Array.IndexOf(counters, mostFrequent), mostFrequent);
    }
}
